package controllers

import (
	// "bytes"
	"cybeego/models"
	"cybeego/myutils"
	"reflect"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	// "github.com/gomodule/redigo/redis"
)

type TaskController struct {
	beego.Controller
}

func (c *TaskController) ShowTask() {

	// c.Data["username"] = c.GetSession("username")
	// c.Data["userid"] = c.GetSession("userid")
	// c.TplName = "task_show.html"
	// var types []models.Task
	// var typesfilter []models.Task
	// o := orm.NewOrm()
	// o.QueryTable("task").All(&types)
	// o.QueryTable("task").Filter("User", c.GetSession("userid")).All(&typesfilter)
	// // o.QueryTable("task").Filter("User", c.GetSession("userid")).One(typesfilter)
	// if c.GetSession("userid") == 1 {
	// 	c.Data["types"] = types
	// } else {
	// 	c.Data["types"] = typesfilter
	// }
	//刷新页面时更新缓存。
	// err := updateRedisDate("set", "domaintypes", types, 300)
	// if err != nil {
	// 	logs.Info("更新缓存失败：", err)
	// }
}

func (c *TaskController) ShowAddTask() {
	c.TplName = "task_show.html"
	c.Data["username"] = c.GetSession("username")
	c.Data["userid"] = c.GetSession("userid")
	var types []models.Task
	var typesfilter []models.Task
	o := orm.NewOrm()
	o.QueryTable("task").All(&types)
	o.QueryTable("task").Filter("TaskUser", c.GetSession("userid")).All(&typesfilter)
	// o.QueryTable("task").Filter("User", c.GetSession("userid")).One(typesfilter)
	if c.GetSession("userid") == 1 {
		c.Data["types"] = types
	} else {
		c.Data["types"] = typesfilter
	}

	// 对比数据类型
	//  task1: [{1 项目一 0xc000252500 []} {2 刘学梁项目一 0xc000252640 []}]
	var task1 []models.DomainType // 切片Slice 适合多组数据
	// task2: {1 项目一 0xc000252780 []}
	var task2 models.DomainType // 查单个Model 多了也差不出来貌似
	logs.Debug("task1 数据类型：", reflect.TypeOf(task1))
	logs.Debug("task2 数据类型：", reflect.TypeOf(task2))
	o.QueryTable("domain_type").All(&task1)
	o.QueryTable("domain_type").All(&task2) // 适合直接Read或者One
	logs.Debug("task1:", task1)
	logs.Debug("task2:", task2)
}

func (c *TaskController) AddTask() {
	// c.TplName = "task_show.html"
	c.Data["username"] = c.GetSession("username")
	c.Data["userid"] = c.GetSession("userid")
	var task models.Task
	if task.Content = c.GetString("Content"); task.Content == "" {
		logs.Info("项目类型不能为空")
		c.Redirect("/Task/ShowTask", 302)
		return
	}
	logs.Info("您输入的项目类型名为：", task.Id, task.Content)
	o := orm.NewOrm()
	myuser := models.User{}
	qs := o.QueryTable("user")
	qs.Filter("Id", c.GetSession("userid")).One(&myuser)
	task.Content = c.GetString("Content")
	task.Title = c.GetString("Title")
	// time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
	task.LastTime = time.Now()
	// task.LastTime = time.Now().Format("2020-01-01 00:00:00")
	task.TaskUser = &myuser

	_, err := o.Insert(&task)
	if err != nil {
		logs.Info("插入数据失败：", err)
		return
	}
	logs.Debug("c.GetString(Content)", c.GetString("Content"))
	myutils.AddTask(task.Title, task.Content, "default")
	c.Redirect("/Task/ShowTask", 302)
}

func (c *TaskController) DeleteTask() {
	// c.TplName = "task_show.html"
	// c.Data["username"] = c.GetSession("username")
	id, err := c.GetInt("id")
	if err != nil {
		logs.Info("获取ID失败：", err)
		return
	}

	// 初始化 task
	task := models.Task{Id: id}
	o := orm.NewOrm()
	// 不查询一下删不掉 不过Filter比较麻烦
	// o.QueryTable("task").Filter("Id", id).One(&task)
	// Read简单一些 已知主键 读取task对象 Read 默认通过查询主键赋值，可以使用指定的字段进行查询：
	o.Read(&task)

	//删除定时任务
	myutils.DelTask(task.Title)
	logs.Info("DeleteTask id", id)
	// 删除数据库表记录
	o.Delete(&task)
	c.Redirect("/Task/ShowTask", 302)
}
